<?php
switch ($action) {
    case 'regist':
        $username = trim($username);
        $password = md5(trim($password));
        if(empty($username) || empty($password)){
            $status = '0';
            $msg = '请正确输入用户名/密码';
        }else{
            $sql = "SELECT count(*) FROM system_user WHERE username='{$username}'";
            if($db->result_first($sql) > 0){
                $status = '0';
                $msg = '用户名重复';
            }else{
                $sql = "INSERT INTO system_user(`username`,password`,`admin_type`,`rank_type`,`give_type`,`status`) VALUES('{$username}','{$password}','0','0','0','OK')";
                $db->query($sql);
                $status = '1';
                $msg = '';
                $userid = $db->insert_id();
                dsetcookie('auth', authcode("{$userid}\t{$password}", 'ENCODE'));
                dsetcookie('username',urlencode($loginuser['username']),86400 * 30);
                dsetcookie('usertype',urlencode($usertype_list[20]),86400 * 30);
            }
        }
        break;
    case 'login':
        $username = trim($username);
        $password = md5(trim($password));
        $loginuser = $db->fetch_first("SELECT `id`,`username`,`admin_type`,`rank_type`,`give_type`,`status` FROM `system_user` WHERE `username`='{$username}' and `password`='{$password}'");
        if ($loginuser) {
            switch($loginuser['status']){
                case 'OK':
                    $userid = $loginuser['id'];
                    dsetcookie('auth', authcode("$userid\t$password", 'ENCODE'));
                    $user_info = array2json(array(
                        'username'=>$loginuser['username'],
                        'admin_type'=>$admin_type_list[$loginuser['admin_type']],
                        'rank_type'=>$rank_type_list[$loginuser['rank_type']],
                        'give_type'=>$give_type_list[$loginuser['give_type']]
                    ));
                    dsetcookie('userinfo',$user_info,86400 * 30);
                    $status = '1';
                    $msg = $user_info;
                    break;
                case 'DISABLED':
                    $status = '0';
                    $msg = '对不起，您的帐号已被禁用';
                    break;
                case 'DEL':
                    $status = '0';
                    $msg = '对不起，您的帐号已被删除';
                    break;
                default:
                    $status = '0';
                    $msg = '对不起，登入失败';
                    break;
            }
        } else {
            $status = '0';
            $msg = '对不起，账号信息有错';
        }
        break;
    case 'logout':
        clearcookies();
        $status = '1';
        $msg = '';
        break;
    default:
        $status = '0';
        $msg = '参数错误';
        break;
}
echo ajax_return($status, $msg);
exit();
?>
